package com.rbac.admin.controller;

import com.rbac.admin.common.Result;
import com.rbac.admin.entity.SysMenu;
import com.rbac.admin.service.SysMenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单控制器
 */
@RestController
@RequestMapping("/api/menus")
@Tag(name="菜单控制器", description = "菜单控制器")
public class MenuController {

    @Autowired
    private SysMenuService menuService;

    /**
     * 获取菜单树
     * @return 菜单树
     */
    @Operation(summary = "获取菜单树")
    @GetMapping("/tree")
    @PreAuthorize("hasAuthority('sys:menu:list')")
    public Result<List<SysMenu>> getMenuTree() {
        List<SysMenu> menuTree = menuService.getMenuTree();
        return Result.success(menuTree);
    }

    /**
     * 获取当前用户菜单树
     * @return 菜单树
     */
    @Operation(summary = "获取当前用户菜单树")
    @GetMapping("/user/tree")
    public Result<List<SysMenu>> getCurrentUserMenuTree() {
        // 从SecurityContext中获取当前用户ID
        Long userId = 1L; // 实际项目中应从SecurityContext获取
        List<SysMenu> menuTree = menuService.getMenuTreeByUserId(userId);
        return Result.success(menuTree);
    }

    /**
     * 根据角色ID获取菜单树
     * @param roleId 角色ID
     * @return 菜单树
     */
    @Operation(summary = "根据角色ID获取菜单树")
    @GetMapping("/role/{roleId}/tree")
    @PreAuthorize("hasAuthority('sys:menu:list')")
    public Result<List<SysMenu>> getMenuTreeByRoleId(@PathVariable Long roleId) {
        List<SysMenu> menuTree = menuService.getMenuTreeByRoleId(roleId);
        return Result.success(menuTree);
    }

    /**
     * 获取菜单详情
     * @param id 菜单ID
     * @return 菜单详情
     */
    @Operation(summary = "获取菜单详情")
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('sys:menu:list')")
    public Result<SysMenu> getMenuById(@PathVariable Long id) {
        SysMenu menu = menuService.getById(id);
        return Result.success(menu);
    }

    /**
     * 添加菜单
     * @param menu 菜单信息
     * @return 添加结果
     */
    @Operation(summary = "添加菜单")
    @PostMapping
    @PreAuthorize("hasAuthority('sys:menu:add')")
    public Result<Boolean> addMenu(@Validated @RequestBody SysMenu menu) {
        boolean result = menuService.save(menu);
        return Result.success(result);
    }

    /**
     * 更新菜单
     * @param id 菜单ID
     * @param menu 菜单信息
     * @return 更新结果
     */
    @Operation(summary = "更新菜单")
    @PutMapping("/{id}")
    @PreAuthorize("hasAuthority('sys:menu:update')")
    public Result<Boolean> updateMenu(@PathVariable Long id, @Validated @RequestBody SysMenu menu) {
        menu.setId(id);
        boolean result = menuService.updateById(menu);
        return Result.success(result);
    }

    /**
     * 删除菜单
     * @param id 菜单ID
     * @return 删除结果
     */
    @Operation(summary = "删除菜单")
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('sys:menu:delete')")
    public Result<Boolean> deleteMenu(@PathVariable Long id) {
        boolean result = menuService.removeById(id);
        return Result.success(result);
    }
}